import pygame

class ammo_bar:
    def __init__(self, game, count, per_dot=1):
        self.game = game
        self.game_logic = game.game_logic
        self.ammo_full = count
        self.ammo = self.ammo_full
        self.per_dot = per_dot
        self._draw_bar()
        
        self.dirty = True
    
    def _draw_bar(self):
        width = ((self.ammo / self.per_dot) * 2) * 4
        if width < 0:
            width = 0;
        self.image = pygame.Surface((width, 4))
        self.image.fill((255, 0, 255))
        self.image.set_colorkey((255, 0, 255))
        self.rect = self.image.get_rect()
        
        self.bullet = pygame.Surface((4, 4))
        self.bullet.fill((0,0,0))
        self.bullet_rect = self.bullet.get_rect()
        
        for i in range(self.ammo):
            movX = i*8
            self.image.blit(self.bullet, self.bullet_rect.move(movX, 0))
        
        self.rect.bottom = self.game_logic.screen_rect.bottom - 15
        self.rect.left = 5
        
    def update(self):
        if self.dirty:
            self._draw_bar()
            
            self.dirty = False
    
    def use_bullet(self):
        self.ammo-= 1
        self.dirty = True
        
    def reload(self):
        self.ammo = self.ammo_full
        self.dirty = True
    
    def set_bullets(self, count):
        self.ammo = count
        self.dirty = True
